﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class administrator_menu_news : Page
{
    dulieuDataContext db=new dulieuDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
      // if(!IsPostBack)
       {
           LoadMenu();
       }
    }
    protected void AddMenu(object s,ImageClickEventArgs e)
    {
        Cate cateNews=new Cate(){Parent = 0,Name = txtmenu.Text,Status = true,TypeCateID = 1};
        db.Cates.InsertOnSubmit(cateNews);
        db.SubmitChanges();
        System.Threading.Thread.Sleep(800);
        lblthongbao.Text = "Thêm thành công";
        LoadMenu();
    }
    protected void UpdateMenu(object s,ImageClickEventArgs e)
    {
        var cate = (from o in db.Cates where o.Cate_ID == Convert.ToInt32(Session["idmenu"]) select o).SingleOrDefault();
        if (cate != null) cate.Name = txtmenu.Text;
        db.SubmitChanges();
        System.Threading.Thread.Sleep(800);
        LoadMenu();
        lblthongbao.Text = "Cập nhật thành công";
    }

    protected void LoadMenu()
    {
        rptmnu.DataSource = from o in db.Cates where o.TypeCateID == 1 select o;
        rptmnu.DataBind();
    }
    protected void UpdateCheck(object s,RepeaterCommandEventArgs e)
    {
        if(e.CommandName=="edit")
        {
            Label lblid =(Label) e.Item.FindControl("lblid");
            Session.Add("idmenu",lblid.Text);
            var cate = (from o in db.Cates where o.Cate_ID == Convert.ToInt32(lblid.Text) select o).SingleOrDefault();
            if (cate != null) txtmenu.Text = cate.Name;
            System.Threading.Thread.Sleep(800);
            LoadMenu();
        }
    }
    protected void DeleteMenu(object s,ImageClickEventArgs e)
    {
        foreach (RepeaterItem item in rptmnu.Items)
        {
            CheckBox chk = (CheckBox) item.FindControl("chk");
            if(chk.Checked)
            {
                Label lblid = (Label)item.FindControl("lblid");
                var cate = (from o in db.Cates where o.Cate_ID == Convert.ToInt32(lblid.Text) select o).SingleOrDefault();
                if (cate != null) db.Cates.DeleteOnSubmit(cate);
                db.SubmitChanges();
            }
        }
        System.Threading.Thread.Sleep(800);
        lblthongbao.Text = "Xóa thành công";
        LoadMenu();
    }
}